package cn.gan.loveofsoul.controller;

import cn.gan.loveofsoul.commons.result.ConfigContents;
import cn.gan.loveofsoul.commons.result.ErrorMessage;
import cn.gan.loveofsoul.commons.result.Result;
import cn.gan.loveofsoul.commons.result.StatusCode;
import cn.gan.loveofsoul.entity.CommemorateDay;
import cn.gan.loveofsoul.entity.SysConfig;
import cn.gan.loveofsoul.entity.User;
import cn.gan.loveofsoul.entity.constants.StatusEnum;
import cn.gan.loveofsoul.entity.vo.CommemorateDayVO;
import cn.gan.loveofsoul.exception.BusinessException;
import cn.gan.loveofsoul.service.ICommemorateDayService;
import cn.gan.loveofsoul.service.ISysConfigService;
import cn.gan.loveofsoul.service.IUserService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;


import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ganwh
 * @since 2023-10-26
 */
@Controller
@RequestMapping("/commemorateDay")
public class CommemorateDayController {
    @Autowired
    private ICommemorateDayService commemorateDayService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 功能描述: 查询用户纪念日资料
     *
     * @param userId
     * @return cn.gan.loveofsoul.commons.result.Result
     * @author ganwh 2023/11/2 18:16
     */
    @GetMapping("/queryCommemorateDay")
    @ResponseBody
    public Result queryUserCommemorateDayInfo(Integer userId) {
        if (Objects.isNull(userId)) {
            throw new BusinessException(StatusCode.BAD_REQUEST, ErrorMessage.MISSING_PARAMETER);
        }
        return commemorateDayService.queryUserCommemorateDayInfo(userId);
    }

    /**
     * 功能描述: 根据ID查询纪念日
     *
     * @param id
     * @return cn.gan.loveofsoul.commons.result.Result
     * @author ganwh 2023/11/6 9:54
     */
    @GetMapping("/getById")
    @ResponseBody
    public Result getById(Integer id) {
        if (Objects.isNull(id)) {
            throw new BusinessException(StatusCode.BAD_REQUEST, ErrorMessage.MISSING_PARAMETER);
        }
        CommemorateDay commemorateDay = commemorateDayService.getById(id);
        if (Objects.isNull(commemorateDay)) {
            return new Result(StatusCode.NOT_FOUND, ErrorMessage.NOT_FOUND);
        }
        if (Objects.nonNull(commemorateDay.getUserId())) {
            User user = userService.getById(commemorateDay.getUserId());
            commemorateDay.setRemark(user.getUserName() + commemorateDay.getRemark());
        }
        return new Result(StatusCode.SUCCESS, ErrorMessage.SUCCESS, commemorateDay);
    }

    /**
     * 功能描述: 新增/修改纪念日
     *
     * @param commemorateDayVO
     * @return cn.gan.loveofsoul.commons.result.Result
     * @author ganwh 2023/11/6 11:09
     */
    @PutMapping("/save")
    @ResponseBody
    public Result save(@RequestBody @Valid CommemorateDayVO commemorateDayVO) {
        CommemorateDay newCommemorateDay;
        if (Objects.nonNull(commemorateDayVO.getId())) {
            newCommemorateDay = commemorateDayService.getById(commemorateDayVO.getId());
            if (Objects.isNull(newCommemorateDay)) {
                return new Result(StatusCode.NOT_FOUND, ErrorMessage.NOT_FOUND);
            }
            BeanUtil.copyProperties(commemorateDayVO, newCommemorateDay);
        } else {
            LambdaQueryWrapper<CommemorateDay> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CommemorateDay::getRelationshipId, commemorateDayVO.getRelationshipId());
            queryWrapper.eq(CommemorateDay::getRemark, commemorateDayVO.getRemark());
            long count = commemorateDayService.count(queryWrapper);
            if (count > 0) {
                return new Result(StatusCode.ERROR, "纪念日名称已存在");
            }
            newCommemorateDay = BeanUtil.copyProperties(commemorateDayVO, CommemorateDay.class);
            newCommemorateDay.setCreateTime(LocalDateTime.now());
        }
        newCommemorateDay.setUpdateTime(LocalDateTime.now());

        if (ConfigContents.CommemorateDayType.BIRTHDAY.equals(newCommemorateDay.getType())) {
            LambdaQueryWrapper<SysConfig> sysConfigWrapper = new LambdaQueryWrapper<>();
            sysConfigWrapper.eq(SysConfig::getCfgKey, ConfigContents.SysConfigKey.USER_CONFIG);
            sysConfigWrapper.eq(SysConfig::getCfgType, ConfigContents.SysConfigType.COMMEMORATE_DAY);
            sysConfigWrapper.eq(SysConfig::getCfgValue, ConfigContents.CommemorateDayType.BIRTHDAY);
            sysConfigWrapper.eq(SysConfig::getStatus, StatusEnum.NORMAL.getCode());
            SysConfig sysConfig = sysConfigService.getOne(sysConfigWrapper);
            newCommemorateDay.setRemark(sysConfig.getRemark());
        }

        commemorateDayService.saveOrUpdate(newCommemorateDay);
        return new Result(StatusCode.SUCCESS, ErrorMessage.SUCCESS);
    }
}
